package org.rsbot.script.methods;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.rsbot.bot.Bot;
import org.rsbot.client.Client;
import org.rsbot.client.MenuGroupNode;
import org.rsbot.client.MenuItemNode;
import org.rsbot.event.EventManager;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.internal.wrappers.Deque;
import org.rsbot.script.internal.wrappers.Queue;

public class Menu extends MethodProvider
{
  private static final Pattern HTML_TAG = Pattern.compile("(^[^<]+>|<[^>]+>|<[^>]+$)");

  private final Object menuCacheLock = new Object();

  private String[] menuOptionsCache = new String[0];
  private String[] menuActionsCache = new String[0];

  private boolean menuListenerStarted = false;

  Menu(MethodContext paramMethodContext) {
    super(paramMethodContext);
  }

  public boolean doAction(String paramString)
  {
    paramString = paramString.toLowerCase();
    int i = getIndex(paramString);
    if (!isOpen()) {
      if (i == -1) {
        return false;
      }
      if (i == 0) {
        this.methods.mouse.click(true);
        return true;
      }
      this.methods.mouse.click(false);
      return clickIndex(i);
    }if (i == -1) {
      while (isOpen()) {
        this.methods.mouse.moveRandomly(750);
        sleep(random(100, 500));
      }
      return false;
    }
    return clickIndex(i);
  }

  public boolean contains(String paramString)
  {
    return getIndex(paramString) != -1;
  }

  public boolean clickIndex(int paramInt)
  {
    if (!isOpen()) {
      return false;
    }
    String[] arrayOfString = getItems();
    if (arrayOfString.length <= paramInt) {
      return false;
    }
    if (isCollapsed()) {
      Queue localQueue1 = new Queue(this.methods.client.getCollapsedMenuItems());
      int i = 0; int j = 0;
      for (MenuGroupNode localMenuGroupNode = (MenuGroupNode)localQueue1.getHead(); localMenuGroupNode != null; j++) {
        Queue localQueue2 = new Queue(localMenuGroupNode.getItems());
        int k = 0;
        for (MenuItemNode localMenuItemNode = (MenuItemNode)localQueue2.getHead(); localMenuItemNode != null; k++) {
          if (i++ == paramInt) {
            if (localQueue2.size() == 1) {
              return clickMain(arrayOfString, j);
            }
            return clickSub(arrayOfString, j, k);
          }
          localMenuItemNode = (MenuItemNode)localQueue2.getNext();
        }
        localMenuGroupNode = (MenuGroupNode)localQueue1.getNext();
      }

      return false;
    }
    return clickMain(arrayOfString, paramInt);
  }

  private boolean clickMain(String[] paramArrayOfString, int paramInt)
  {
    Point localPoint = getLocation();
    int i = random(4, paramArrayOfString[paramInt].length() * 4);
    int j = 21 + 16 * paramInt + random(3, 12);
    this.methods.mouse.move(localPoint.x + i, localPoint.y + j, 2, 2);
    if (isOpen()) {
      this.methods.mouse.click(true);
      return true;
    }
    return false;
  }

  private boolean clickSub(String[] paramArrayOfString, int paramInt1, int paramInt2) {
    Point localPoint1 = getLocation();
    int i = random(4, paramArrayOfString[paramInt1].length() * 4);
    int j = 21 + 16 * paramInt1 + random(3, 12);
    this.methods.mouse.move(localPoint1.x + i, localPoint1.y + j, 2, 2);
    sleep(random(125, 150));

    if (isOpen()) {
      Point localPoint2 = getSubMenuLocation();
      i = random(4, paramArrayOfString[paramInt2].length() * 4);
      this.methods.mouse.move(localPoint2.x + i, this.methods.mouse.getLocation().y, 2, 0);
      sleep(random(125, 150));

      if (isOpen()) {
        j = 16 * paramInt2 + random(3, 12) + 21;
        this.methods.mouse.move(this.methods.mouse.getLocation().x, localPoint2.y + j, 0, 2);
        sleep(random(125, 150));
        if (isOpen()) {
          this.methods.mouse.click(true);
          return true;
        }
      }
    }
    return false;
  }

  public String[] getActions()
  {
    return getMenuItemPart(true);
  }

  public int getIndex(String paramString)
  {
    paramString = paramString.toLowerCase();
    String[] arrayOfString = getItems();
    for (int i = 0; i < arrayOfString.length; i++) {
      if (arrayOfString[i].toLowerCase().contains(paramString)) {
        return i;
      }
    }
    return -1;
  }

  public String[] getItems()
  {
    String[] arrayOfString1;
    String[] arrayOfString2;
    synchronized (this.menuCacheLock) {
      arrayOfString1 = this.menuOptionsCache;
      arrayOfString2 = this.menuActionsCache;
    }

    ??? = new ArrayList();

    int i = Math.min(arrayOfString1.length, arrayOfString2.length);
    for (int j = 0; j < i; j++) {
      String str1 = arrayOfString1[j];
      String str2 = arrayOfString2[j];
      if ((str1 != null) && (str2 != null)) {
        String str3 = str2 + " " + str1;
        ((ArrayList)???).add(str3.trim());
      }
    }

    if ((((ArrayList)???).size() > 1) && (((String)((ArrayList)???).get(0)).equals("Cancel"))) {
      Collections.reverse((List)???);
    }

    return (String)(String[])((ArrayList)???).toArray(new String[((ArrayList)???).size()]);
  }

  public Point getLocation()
  {
    if (isOpen()) {
      return new Point(this.methods.client.getMenuX(), this.methods.client.getMenuY());
    }
    return null;
  }

  private String[] getMenuItemPart(boolean paramBoolean) {
    LinkedList localLinkedList = new LinkedList();
    MenuItemNode localMenuItemNode;
    if (isCollapsed()) {
      localObject1 = new Queue(this.methods.client.getCollapsedMenuItems());
      for (localObject2 = (MenuGroupNode)((Queue)localObject1).getHead(); localObject2 != null; localObject2 = (MenuGroupNode)((Queue)localObject1).getNext()) {
        Queue localQueue = new Queue(((MenuGroupNode)localObject2).getItems());
        for (localMenuItemNode = (MenuItemNode)localQueue.getHead(); localMenuItemNode != null; localMenuItemNode = (MenuItemNode)localQueue.getNext())
          localLinkedList.add(paramBoolean ? localMenuItemNode.getAction() : localMenuItemNode.getOption());
      }
    }
    else {
      localObject1 = new Deque(this.methods.client.getMenuItems());
      for (localObject2 = (MenuItemNode)((Deque)localObject1).getHead(); localObject2 != null; localObject2 = (MenuItemNode)((Deque)localObject1).getNext()) {
        localLinkedList.add(paramBoolean ? ((MenuItemNode)localObject2).getAction() : ((MenuItemNode)localObject2).getOption());
      }
    }
    Object localObject1 = (String[])localLinkedList.toArray(new String[localLinkedList.size()]);
    Object localObject2 = new LinkedList();
    int i;
    if (isCollapsed())
      for (i = 0; i < localObject1.length; i++) {
        localMenuItemNode = localObject1[i];
        ((LinkedList)localObject2).add(localMenuItemNode == null ? "" : stripFormatting(localMenuItemNode));
      }
    else {
      for (i = localObject1.length - 1; i >= 0; i--) {
        localMenuItemNode = localObject1[i];
        ((LinkedList)localObject2).add(localMenuItemNode == null ? "" : stripFormatting(localMenuItemNode));
      }
    }
    return (String)(String)(String[])((LinkedList)localObject2).toArray(new String[((LinkedList)localObject2).size()]);
  }

  public String[] getOptions()
  {
    return getMenuItemPart(false);
  }

  public int getSize()
  {
    return getItems().length;
  }

  public Point getSubMenuLocation()
  {
    if (isCollapsed()) {
      return new Point(this.methods.client.getSubMenuX() + 4, this.methods.client.getSubMenuY() + 4);
    }
    return null;
  }

  public boolean isCollapsed()
  {
    return this.methods.client.isMenuCollapsed();
  }

  public boolean isOpen()
  {
    return this.methods.client.isMenuOpen();
  }

  public void setupListener()
  {
    if (this.menuListenerStarted) {
      return;
    }
    this.menuListenerStarted = true;
    this.methods.bot.getEventManager().addListener(new Object()
    {
      public void onRepaint(Graphics paramGraphics) {
        synchronized (Menu.this.menuCacheLock) {
          Menu.access$102(Menu.this, Menu.this.getOptions());
          Menu.access$202(Menu.this, Menu.this.getActions());
        }
      }
    }
    , 4096L);
  }

  private String stripFormatting(String paramString)
  {
    return HTML_TAG.matcher(paramString).replaceAll("");
  }
}